<?php

class Payment_Weixin_Refund 
{
    // 从微信支付给订单退款：原路返回
    public function order ($orderNo, $out_trade_no, $total_fee, $refund_fee)
    {
        if ($total_fee <= 0 || $refund_fee <= 0 || $refund_fee > $total_fee)
            return FALSE;

	    try {
		require_once DOCROOT."/resource/wxpay/lib/WxPay.Api.php";

		$input = new WxPayRefund();
		$input->SetOut_trade_no($out_trade_no);
		$input->SetTotal_fee($total_fee);
		$input->SetRefund_fee($refund_fee);
	    $input->SetOut_refund_no($orderNo); // . date("YmdHis"));
	    $input->SetOp_user_id(WxPayConfig::MCHID);
		$result = WxPayApi::refund($input, 15);
	    $returnCode = Arr::get($result, 'return_code');

	    Kohana::$log->add(Log::DEBUG, "WXPAY(MP)_REFUND_RESULT:". print_r($result, TRUE));
	
        return $returnCode == 'SUCCESS';

	    }
	    catch (Exception $e)
	    {
            Kohana::$log->add(Log::ERROR, "WXPAY(MP)_REFUND_ERROR:".$e->getMessage());
            return FALSE; 
	    }
    }
}

